package com.hex.ds.hdtp.core.inf.data.table.repository.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.hex.ds.hdtp.core.inf.common.page.Page;
import com.hex.ds.hdtp.core.inf.common.page.converter.IPageAndMyBatisPlusPageConverter;
import com.hex.ds.hdtp.core.inf.data.table.mapper.ITableTransferListMapper;
import com.hex.ds.hdtp.core.inf.data.table.po.TableTransferListPo;
import com.hex.ds.hdtp.core.inf.data.table.repository.ITableTransferListRepository;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * @Package com.hex.ds.hdtp.core.inf.data.table.repository.impl
 * @ClassName TableTransferListRepository
 * @Description 表迁移清单-数据接口实现类
 * @Author Wang zhihao
 * @Date 2023/8/9 11:28
 * @Version v1.0
 **/
@Component
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class TableTransferListRepository implements ITableTransferListRepository {

    private final ITableTransferListMapper tableTransferListMapper;

    private final IPageAndMyBatisPlusPageConverter pageConverter;

    /**
     * @Method queryListByPage
     * @Param
     * @param tableTransferListPo
     * @param toPage
     * @Return com.hex.ds.hdtp.core.inf.common.page.Page<com.hex.ds.hdtp.core.inf.data.table.po.TableTransferListPo>
     * @Description 分页查询
     * @Author Wang zhihao
     * @Date 2023/8/9 11:24
     * @Version V1.0
     */
    @Override
    public Page<TableTransferListPo> queryListByPage(TableTransferListPo tableTransferListPo, Page toPage) {
        final IPage tmpPageReslt = tableTransferListMapper.queryListByPage(
                pageConverter.toMyBatisPlusPage(toPage)
                , tableTransferListPo
        );
        final List list = tmpPageReslt.getRecords();
        final Page pageResult = pageConverter.toPage(tmpPageReslt);
        return pageResult.setData(list);
    }

    /**
     * @Method queryPageListByTaskId
     * @Param tableTransferListPo
     * @Param toPage
     * @Return com.hex.ds.hdtp.core.inf.common.page.Page<com.hex.ds.hdtp.core.inf.data.table.po.TableTransferListPo>
     * @Description 根据任务主键分页查询
     * @Author gj.xu
     * @Date 2024/3/8 18:30
     * @Version V2.0
     */
    @Override
    public Page<TableTransferListPo> queryPageListByTaskId(TableTransferListPo tableTransferListPo, Page toPage) {
        final IPage tmpPageReslt = tableTransferListMapper.queryPageListByTaskId(
                pageConverter.toMyBatisPlusPage(toPage)
                , tableTransferListPo
        );
        final List<TableTransferListPo> list = tmpPageReslt.getRecords();
        final Page pageResult = pageConverter.toPage(tmpPageReslt);
        return pageResult.setData(list);
    }

    /**
     * @Method queryList
     * @Param
     * @param tableTransferListPo
     * @Return java.util.List<com.hex.ds.hdtp.core.inf.data.table.po.TableTransferListPo>
     * @Description 不分页查询
     * @Author Wang zhihao
     * @Date 2023/8/10 14:30
     * @Version V1.0
     */
    @Override
    public List<TableTransferListPo> queryList(TableTransferListPo tableTransferListPo) {
        return tableTransferListMapper.queryList(tableTransferListPo);
    }

    /**
     * @Method queryLatestTransferTable
     * @Param
     * @Param sourceDbName
     * @Param sourceTableName
     * @Return com.hex.ds.hdtp.core.inf.data.table.po.TableTransferListPo
     * @Description 根据源库名表名查询最新的迁移表信息
     * @Author gj.xu
     * @Date 2023/8/29 13:38
     * @Version V2.0
     */
    @Override
    public TableTransferListPo queryLatestTransferTable(String sourceDbName, String sourceTableName) {
        return tableTransferListMapper.queryLatestTransferTable(sourceDbName, sourceTableName);
    }

    /**
     * @Method queryLatestTransferTableByTarget
     * @Param
     * @Param targetDbName
     * @Param targetTableName
     * @Return com.hex.ds.hdtp.core.inf.data.table.po.TableTransferListPo
     * @Description 根据目标库名表名查询最新的迁移表信息
     * @Author Yin.Yang
     * @Date 2023/9/27 16:42
     * @Version V1.0
     */
    @Override
    public TableTransferListPo queryLatestTransferTableByTarget(String targetDbName, String targetTableName, String targetPkId) {
        return tableTransferListMapper.queryLatestTransferTableByTarget(targetDbName, targetTableName, targetPkId);
    }

    /**
     * @Method removeByTaskPkId
     * @Param
     * @param taskPkId
     * @Return void
     * @Description 表迁移清单-根据任务主键删除
     * @Author Wang zhihao
     * @Date 2023/8/9 19:34
     * @Version V1.0
     */
    @Override
    public void removeByTaskPkId(String taskPkId) {
        tableTransferListMapper.deleteByTaskPkId(taskPkId);
    }

    /**
     * @Method addWithBackId
     * @Param
     * @param tableTransferListPo
     * @Return java.lang.String
     * @Description 新增并返回主键
     * @Author Wang zhihao
     * @Date 2023/8/14 10:05
     * @Version V1.0
     */
    @Override
    public String addWithBackId(TableTransferListPo tableTransferListPo) {
        tableTransferListMapper.insert(tableTransferListPo);
        return tableTransferListPo.getPkId();
    }

    /**
     * @Method queryLatestSuccess
     * @Param
     * @param tableTransferListPo
     * @param toPage
     * @Return com.hex.ds.hdtp.core.inf.common.page.Page<com.hex.ds.hdtp.core.inf.data.table.po.TableTransferListPo>
     * @Description 表迁移清单-分页查询每个建表最新成功的
     * @Author Wang zhihao
     * @Date 2023/8/28 11:13
     * @Version V1.0
     */
    @Override
    public Page<TableTransferListPo> queryLatestSuccess(TableTransferListPo tableTransferListPo, Page toPage) {
        final IPage tmpPageReslt = tableTransferListMapper.queryLatestSuccess(
                pageConverter.toMyBatisPlusPage(toPage)
                , tableTransferListPo
        );
        final List list = tmpPageReslt.getRecords();
        final Page pageResult = pageConverter.toPage(tmpPageReslt);
        return pageResult.setData(list);
    }

    /**
     * @Method queryById
     * @Param
     * @param pkId
     * @Return com.hex.ds.hdtp.core.inf.data.table.po.TableTransferListPo
     * @Description 根据主键查询
     * @Author Wang zhihao
     * @Date 2023/8/9 11:25
     * @Version V1.0
     */
    @Override
    public TableTransferListPo queryById(String pkId) {
        return tableTransferListMapper.selectById(pkId);
    }

    /**
     * @Method add
     * @Param
     * @param tableTransferListPo
     * @Return java.lang.Integer
     * @Description 新增表迁移清单
     * @Author Wang zhihao
     * @Date 2023/8/9 11:27
     * @Version V1.0
     */
    @Override
    public Integer add(TableTransferListPo tableTransferListPo) {
        return tableTransferListMapper.insert(tableTransferListPo);
    }

    /**
     * @Method modifyById
     * @Param
     * @param tableTransferListPo
     * @Return java.lang.Integer
     * @Description 通过主键修改
     * @Author Wang zhihao
     * @Date 2023/8/9 11:28
     * @Version V1.0
     */
    @Override
    public Integer modifyById(TableTransferListPo tableTransferListPo) {
        return tableTransferListMapper.updateById(tableTransferListPo);
    }

    /**
     * @Method removeById
     * @Param
     * @param pkId
     * @Return java.lang.Integer
     * @Description 通过主键删除
     * @Author Wang zhihao
     * @Date 2023/8/9 11:28
     * @Version V1.0
     */
    @Override
    public Integer removeById(String pkId) {
        return tableTransferListMapper.deleteById(pkId);
    }

}
